package com.baosight.sczx.zm.ce.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.ce.Equipment;
import com.baosight.sczx.common.zm.cInterface.ce.Note;
import com.baosight.sczx.common.zm.cInterface.ce.RerollingStatus;
import com.baosight.sczx.zm.ce.domain.ZMCEWorkPlan;

/**
 * <p>Title: ServiceZMCE01.java</p>
 * <p>Description: 工作计划</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-12-13 下午02:23:03  张小飞  Create
 *</pre>
 */
public class ServiceZMCE01 extends ServiceEPBase {
	
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-13下午02:23:37 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-13下午02:23:37 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdPreparerDepId", UserSession.getInSessionProperty("orgCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCEWorkPlan.query", new ZMCEWorkPlan());
		
		outInfo.addBlock(Equipment.getEquipmentBlock("cmes.ce.equipment", null));
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.ce.departmentWithSpace", "orgCode", "orgName", true));		
		outInfo.addBlock(Note.getNoteBlock("cmes.ce.note", null));
		
		return outInfo;
	}

	/**
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-13下午02:23:37 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validate = this.validate(inInfo, "ZMCEWorkPlan.validate", new HashMap<String, String>());
		if(validate) {
			return inInfo;
		}
		else {
			return insertAgain(inInfo);
		}
	}

	/**
	 * 
	 * <p>Function: insertAgain</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:42:55 张小飞
	 * </p>
	 *
	 */
	public EiInfo insertAgain(EiInfo inInfo) {
		String fdSource = inInfo.getBlock("result").getCellStr(0, "fdSource");
		String fdSourceId = inInfo.getBlock("result").getCellStr(0, "fdSourceId");
		String workrecordId = inInfo.getBlock("result").getCellStr(0, "workrecordId");
		
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CE_WORKPLAN"));
		inInfo.getBlock("result").setCell(0, "fdIsExtension", "否");
		inInfo.getBlock("result").setCell(0, "fdPreparerId", UserSession.getUserId());
		inInfo.getBlock("result").setCell(0, "fdPreparer", UserSession.getInSessionProperty("UserCname"));
		inInfo.getBlock("result").setCell(0, "fdPreparerDepId", UserSession.getInSessionProperty("orgCode"));
		if("计划".equals(inInfo.getBlock("result").getCell(0, "fdStatus")))
		{
			inInfo.getBlock("result").setCell(0, "fdTrueBeganDate", StaticTools.getDate());
		
			if("大修".equals(inInfo.getBlock("result").getCell(0, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(0, "fdType")))
			{
				if(Equipment.updateEquipmentStatus("待修", "不完好", inInfo.getBlock("result").getCellStr(0, "fdEquipmentId"))<=0)
				{
					inInfo.setStatus(-1);
					return inInfo;
				}
			}
		}
		EiInfo outinfo = super.insert(inInfo, "ZMCEWorkPlan.insert");
		
		//异常问题模块中生成计划中调用
		if("异常".equals(fdSource)){
			
			inInfo.getBlock("result").setCell(0, "fdKeyId", fdSourceId);
			inInfo.getBlock("result").setCell(0, "fdStatus", "处理中");
			inInfo.getBlock("result").setCell(0, "fdSolveDepartment", UserSession.getInSessionProperty("orgName"));
			inInfo.getBlock("result").setCell(0, "fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdSolvePerson", UserSession.getInSessionProperty("UserCname"));
			inInfo.getBlock("result").setCell(0, "fdProcessDatetime", StaticTools.getDateTime());
			super.update(inInfo, "ZMCEUnusualProblem.updateAfterGenerate");
			
			inInfo.getBlock("result").setCell(0, "fdKeyId", workrecordId);
			inInfo.getBlock("result").setCell(0, "fdStatus", "处理中");
			super.update(inInfo, "ZMCEFaultRecord.updateStatus");
			
		}
		
		return outinfo;		
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-13下午02:23:37 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validate = this.validate(inInfo, "ZMCEWorkPlan.validate", params);
		
		if(validate) {
			return inInfo;
		}
		else {
			return updateAgain(inInfo);
		}
	}
	
	/**
	 * 
	 * <p>Function: updateAgain</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-16下午04:54:00 张小飞
	 * </p>
	 *
	 */
	public EiInfo updateAgain(EiInfo inInfo) {
		inInfo.getBlock("result").setCell(0, "fdIsExtension", "否");
		inInfo.getBlock("result").setCell(0, "fdModifier", UserSession.getInSessionProperty("UserCname"));
		
		EiInfo outInfo = super.update(inInfo, "ZMCEWorkPlan.update");
		return outInfo;
		
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCE01</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-13下午02:23:37 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCEWorkPlan.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 生效
	 * <p>Function: effect</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-2-21下午02:15:37 张志芳
	 * </p>
	 *
	 */
	public EiInfo effect(EiInfo inInfo) {
		for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
		{
			if("大修".equals(inInfo.getBlock("result").getCell(i, "fdType"))||"维修".equals(inInfo.getBlock("result").getCell(i, "fdType")))
			{
				if(Equipment.updateEquipmentStatus("待修", "不完好", inInfo.getBlock("result").getCellStr(i, "fdEquipmentId"))<=0)
				{
					inInfo.setStatus(-1);
					return inInfo;
				}
			}
			inInfo.getBlock("result").setCell(i, "fdTrueBeganDate", StaticTools.getDate());
		}
		EiInfo _temp = super.update(inInfo, "ZMCEWorkPlan.effect");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: invalid</p>
	 * <p>Description: 作废</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-02-21上午10:35:32 张志芳
	 * </p>
	 *
	 */
	public EiInfo invalid(EiInfo inInfo) {
		RerollingStatus.rerolling(inInfo, "result", 2);
		EiInfo _temp = super.update(inInfo, "ZMCEWorkPlan.invalid");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 校验重复计划编号</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-15上午11:09:26 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdType = inInfo.getBlock("result").getCellStr(0, "fdType");
		String fdEquipmentId = inInfo.getBlock("result").getCellStr(0, "fdEquipmentId");
		String fdBeganDate = inInfo.getBlock("result").getCellStr(0, "fdBeganDate");
		String fdEndDate = inInfo.getBlock("result").getCellStr(0, "fdEndDate");
		
		params.put("fdType", fdType);
		params.put("fdEquipmentId", fdEquipmentId);
		params.put("fdBeganDate", fdBeganDate);
		params.put("fdEndDate", fdEndDate);
		params.put("fdStatus", "作废");
		List<ZMCEWorkPlan> result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.set("mark", "no");
			inInfo.setMsg("该计划时间里已经存在该设备的"+fdType+"工作计划，查看"+result.get(0).getFdCode()+"计划！");
			return true;
		}
		
		params.remove("fdType");
		List<ZMCEWorkPlan> resultNoType = dao.query(sqlName, params);
		if(resultNoType != null && resultNoType.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.set("mark", "may");
			inInfo.setMsg("该计划与"+resultNoType.get(0).getFdCode()+"计划的时间段重叠，确定要提交？");
			return true;
		}
		return false;
	}

}
